package com.mingyizhudao.app.view.mine;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.config.SystemConfig;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.network.Net;
import com.dixiang.framework.utils.AlertDialogs;
import com.dixiang.framework.utils.BitmapCache;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.common.IDataLoadListenter;
import com.mingyizhudao.app.http.DataUtils;
import com.mingyizhudao.app.moudle.bitmap.BimpTempHelper;
import com.mingyizhudao.app.moudle.bitmap.ImageGridActivity;
import com.mingyizhudao.app.moudle.bitmap.ImageItem;
import com.mingyizhudao.app.moudle.bitmap.NoScrollGridView;
import com.mingyizhudao.app.moudle.bitmap.PhotoActivity;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.utils.ToastUtil;
import com.mingyizhudao.app.utils.UploadUtils;
import com.mingyizhudao.app.utils.UploadUtils.OnLoadListener;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.PayViewActivity;
import com.mingyizhudao.app.view.mine.bean.ImgQiNiuEntity;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;

public class OrderStateCommonActivity extends MYBaseActivity{
	private OrderItemEntity orderItemEntity;
	private String id ;//获取订单详情后得到的
	
	private View line ;
	private RelativeLayout rl_yizhifu;
	private LinearLayout ll_zhifu;
	private TextView tv_shengyu ,tv_gozhifu;
	
	private String payUrl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_state_common);
		super.onCreate(savedInstanceState);
		
		//getOrderDetail();
		
	}


	@Override
	protected void onResume() {
		 line = (View)findViewById(R.id.line);
		 rl_yizhifu = (RelativeLayout) findViewById(R.id.rl_yizhifu);
		 ll_zhifu = (LinearLayout) findViewById(R.id.ll_zhifu);
		// line.setVisibility(View.GONE);
		 rl_yizhifu.setVisibility(View.GONE);
		 ll_zhifu.setVisibility(View.GONE);
		getOrderDetail();
		super.onResume();
	}

	@Override
	public void initHeaderView() {
		initBackView( R.drawable.left_back_white_icon, 0, new OnClickListener()
		{
			@Override
			public void onClick( View v )
			{
				finish();
			}
		} );
		setTitleViewValue(R.string.title_order_detail,0,R.color.white);
		setTopViewBg(R.color.actionbar_bg_color);
	}


	private void getOrderDetail(){
		LoadingDialog.getInstance(activity).show();
		String url = getIntent().getStringExtra(Constants.PARM_ACTION_URL);
		DataUtils.getInitial().getBookingDetail(activity, url, null, new IDataLoadListenter() {
			
			@Override
			public void onSuccess(final Object result) {
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						if(result != null){
							orderItemEntity = (OrderItemEntity)result;
								id = orderItemEntity.getId();
								setOrderDetail();
						}else {
							Toast.makeText( activity, "获取数据失败！", 0 );
						}
					}
				});
				
			}
			
			@Override
			public void onFailure(Object result) {
				QjResult<List<OrderItemEntity>> value = (QjResult<List<OrderItemEntity>>) result;
				if(value != null) CommonUtils.showErrorAtBottom(activity, handler, value.getErrorMsg());
			};
			
			@Override
			public void onError(Exception e, Object result) {
				if(e != null) CommonUtils.showErrorAtBottom(activity, handler, e.getMessage());
			}
			
			@Override
			public void onHttpError(int msg) {
				CommonUtils.showHttpError(activity, handler, msg);
			};
			
			@Override
			public void onCompleted(Exception e, Object result) {
				LoadingDialog.getInstance(activity).dismiss();
			}
		});
	}
	
	
	private void setOrderDetail(){
		
		//OrderItemEntity data = (OrderItemEntity) getIntent().getSerializableExtra("orderItemEntity");
		
		
		TextView tv_state = (TextView) findViewById(R.id.tv_state);
		
		TextView tv_yizhifu = (TextView) findViewById(R.id.tv_yizhifu);
		TextView tv_yizhifu_jine = (TextView) findViewById(R.id.tv_yizhifu_jine);
		
		tv_shengyu = (TextView) findViewById(R.id.tv_shengyu);
		tv_gozhifu = (TextView) findViewById(R.id.tv_gozhifu);
		//**************************************************************************************
		
		if ("1".equals(orderItemEntity.getBkStatus())) {
			tv_state.setText("待支付手术预约金");
			
			if (orderItemEntity.getDepositTotalAmount()-orderItemEntity.getDepositAmount()<=0) {
				rl_yizhifu.setVisibility(View.VISIBLE);
				tv_yizhifu.setText("已支付手术预约金：");
				tv_yizhifu_jine.setText(""+orderItemEntity.getDepositAmount()+"元/(共"+orderItemEntity.getDepositTotalAmount()+"元)");
				setgray();
			}else if (orderItemEntity.getDepositTotalAmount()-orderItemEntity.getDepositAmount()>0) {
				if (orderItemEntity.getDepositAmount()>0) {
					rl_yizhifu.setVisibility(View.VISIBLE);
					tv_yizhifu.setText("已支付手术预约金：");
					tv_yizhifu_jine.setText(""+orderItemEntity.getDepositAmount()+"元/(共"+orderItemEntity.getDepositTotalAmount()+"元)");
				}else {
					rl_yizhifu.setVisibility(View.GONE);
				}
				
				//ll_zhifu.setVisibility(View.VISIBLE);
				tv_shengyu.setText(""+(orderItemEntity.getDepositTotalAmount()-orderItemEntity.getDepositAmount()));
				gotoResultView();
				
			}
		}
		
		else if ("2".equals(orderItemEntity.getBkStatus())) {
			tv_state.setText("安排专家中");
			line.setVisibility(View.VISIBLE);
			rl_yizhifu.setVisibility(View.VISIBLE);
			tv_yizhifu.setText("已支付手术预约金：");
			tv_yizhifu_jine.setText(""+(orderItemEntity.getDepositTotalAmount()-orderItemEntity.getDepositAmount())+"元/(共"+orderItemEntity.getDepositTotalAmount()+"元)");
		}
		
		else if ("5".equals(orderItemEntity.getBkStatus())) {
			tv_state.setText("待支付平台服务费");
			
			if (orderItemEntity.getServiceTotalAmount()-orderItemEntity.getServiceAmount()<=0) {
				rl_yizhifu.setVisibility(View.VISIBLE);
				tv_yizhifu.setText("已支付平台服务费：");
				tv_yizhifu_jine.setText(""+orderItemEntity.getServiceAmount()+"元/(共"+orderItemEntity.getServiceTotalAmount()+"元)");
				setgray();
			}else if (orderItemEntity.getServiceTotalAmount()-orderItemEntity.getServiceAmount()>0) {
				if (orderItemEntity.getServiceAmount()>0) {
					rl_yizhifu.setVisibility(View.VISIBLE);
					tv_yizhifu.setText("已支付平台服务费：");
					tv_yizhifu_jine.setText(""+orderItemEntity.getServiceAmount()+"元/(共"+orderItemEntity.getServiceTotalAmount()+"元)");
				}else {
					rl_yizhifu.setVisibility(View.GONE);
				}
				
				//ll_zhifu.setVisibility(View.VISIBLE);
				tv_shengyu.setText(""+(orderItemEntity.getServiceTotalAmount()-orderItemEntity.getServiceAmount()));
				gotoResultView();
				
			}
			
		}
		else if ("9".equals(orderItemEntity.getBkStatus())) {
			tv_state.setText("已取消");
			line.setVisibility(View.VISIBLE);
		}
		else {
			tv_state.setText("已完成");
			line.setVisibility(View.VISIBLE);
			rl_yizhifu.setVisibility(View.VISIBLE);
			tv_yizhifu.setText("总费用：");
			tv_yizhifu_jine.setText(""+(orderItemEntity.getDepositTotalAmount()+orderItemEntity.getServiceTotalAmount()));
		
		}
		
		//***********************************************************************************
		
		((TextView)findViewById(R.id.tv_order_number)).setText( orderItemEntity.getRefNo() );


		TextView tv_hospital_name = (TextView)findViewById(R.id.tv_hospital_name);

		tv_hospital_name.setText( orderItemEntity.getHospitalName() );
		//tv_hospital_name.setText( orderItemEntity.getHpShortName() );//简称

		TextView tv_dept_name = (TextView)findViewById(R.id.tv_dept_name);
		tv_dept_name.setText( orderItemEntity.getHpDeptName() );
		TextView tv_expert_name = (TextView)findViewById(R.id.tv_expert_name);
		
		tv_expert_name.setText(  Utils.isStringEmpty(orderItemEntity.getExpertName()) ? "未填写" : orderItemEntity.getExpertName() );

		TextView tv_casename = (TextView)findViewById(R.id.tv_casename);
		tv_casename.setText( orderItemEntity.getDiseaseName() );
		TextView tv_disease_description = (TextView)findViewById(R.id.tv_disease_description);
		tv_disease_description.setText( orderItemEntity.getDiseaseDetail() );

		
		
		TextView tv_go_details = (TextView)findViewById(R.id.tv_go_details);
		tv_go_details.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
		    IntentHelper.getInstance(activity).gotoActivityWithURL(OrderDetailActivity.class, getIntent().getStringExtra(Constants.PARM_ACTION_URL));
				
			}
		});
		
		
		
	}

    private void setgray() {
    	//ll_zhifu.setVisibility(View.VISIBLE);
    	tv_gozhifu.setClickable(false);
    	tv_shengyu.setBackgroundColor(getResources().getColor(R.color.province_gray_bg));
    	tv_gozhifu.setBackgroundColor(getResources().getColor(R.color.list_item_checked_gray));
    	tv_shengyu.setText("0.0");
	}
	
   
    private void gotoResultView() {
    	 tv_gozhifu.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				payUrl = CommonUtils.getPayUrl(activity, orderItemEntity.getRefNo());
    				Intent intent = new Intent(activity,PayViewActivity.class);
    				Bundle bundle = new Bundle();
    				bundle.putString(Constants.PARM_ACTION_URL, payUrl);
    				bundle.putString(Constants.PARM_PAGE_TITLE, "支付");
    				intent.putExtras(bundle);
    				startActivityForResult(intent, 1);
    				
    			}
    		});
		
	}
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if (requestCode==1) {
			//IntentHelper.getInstance(activity).gotoActivity(OrderListActivity.class);
			//finish();
		}
		
	}
    
	
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		BimpTempHelper.getInstance().clearData();
	}


	
//		    orderTypeWaitFukuan = 1, //待付款
//		    orderTypeAnpai = 2, //安排中
//		    orderTypeWaitQueren = 5, //待确认/支付平台咨询费
//	        orderTypeWaitPingjia = 6, //待评价
//		    orderTypePingjia = 8, //已评价
//		    orderTypeQuxiao = 9, //已取消
//		    
//		    
//		    
//		    const BK_STATUS_NEW = 1;         // 待处理(支付费用)
//		    const BK_STATUS_PROCESSING = 2;   // 处理中（安排中）
//		    const BK_STATUS_CONFIRMED_DOCTOR = 3;   // 已确认专家
//		    const BK_STATUS_PATIENT_ACCEPTED = 4;   // 患者已接受
//		    const BK_STATUS_SERVICE_UNPAID = 5;      // 待支付服务费（确定手术时间）
//		    const BK_STATUS_SERVICE_PAIDED = 6;      // 已支付服务费（入院手术）
//		    //const BK_STATUS_PROCESS_DONE = 7;   // 跟进结束
//		    const BK_STATUS_DONE = 8;         // 已完成
//		    const BK_STATUS_CANCELLED = 9;   // 已取消
//		    const BK_STATUS_REFUNDING = 7;//申请退款
//		    const BK_STATUS_REFUNDED = 10;//已退款
//		    const BK_STATUS_PROCESS_DONE = 11;         // 跟进结束（提交出院小结）
//		    const BK_STATUS_CHECKOUT = 12;   // 已结账（返款）
//		    const BK_STATUS_INVALID = 13;   // 跟进失效
//		    const BK_STATUS_NULLIFY = 14;      // 作废

	
	
}
